﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Presentation
{
    public partial class SetDelay : Form
    {
        public SetDelay()
        {
            InitializeComponent();
        }

        private bool _OK;

        #region

        private void cmdAddSec_Click(object sender, EventArgs e)
        {
            decimal temp = nudAddTime.Value * 1000;
            AddTime(temp);
        }

        private void cmdAddMin_Click(object sender, EventArgs e)
        {
            decimal temp = nudAddTime.Value * 60000;
            AddTime(temp);
        }

        private void cmdAddHours_Click(object sender, EventArgs e)
        {
            decimal temp = nudAddTime.Value * 3600000;
            AddTime(temp);
        }

        private void AddTime(decimal Time)
        {
            if (Time > 0)
            {
                if (nudDelay.Value + Time > nudDelay.Maximum)
                    nudDelay.Value = nudDelay.Maximum;
                else
                    nudDelay.Value += Time;
            }
            else
            {
                if (nudDelay.Value + Time < nudDelay.Minimum)
                    nudDelay.Value = nudDelay.Minimum;
                else
                    nudDelay.Value += Time;
            }
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            _OK = true;
            this.Close();
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void nudDelay_ValueChanged(object sender, EventArgs e)
        {
            int _temp = Convert.ToInt32(nudDelay.Value);
            string _time = null;
            string _time1 = (_temp / 3600000).ToString();
            if (_temp >= 3600000)
            {
                _time += " " + (_temp / 3600000).ToString() + "h";
                _temp = _temp % 3600000;
            }
            _time1 += ":" + (_temp / 60000).ToString();
            if (_temp >= 60000)
            {
                _time += " " + (_temp / 60000).ToString() + "m";
                _temp = _temp % 60000;
            }
            _time1 += ":" + (_temp / 1000).ToString();
            if (_temp >= 1000)
            {
                _time += " " + (_temp / 1000).ToString() + "s";
                _temp = _temp % 1000;
            }
            _time1 += "." + _temp.ToString();
            if (_temp > 0 || _time == null)
            {
                _time += " " + _temp.ToString() + "ms";
            }
            this.Text = "SetDelay:" + _time;
            lbTime.Text = _time1;
        }

        #endregion

        #region

        public int ShowSetDelay(int DelayDefaunt)
        {
            nudDelay.Value = Convert.ToDecimal(DelayDefaunt);
            nudAddTime.Value = 0;
            _OK = false;
            this.ShowDialog();
            if(_OK)
                return Convert.ToInt32(nudDelay.Value);
            return DelayDefaunt;
        }

        public int ShowSetDelay()
        {
            nudDelay.Value = Convert.ToDecimal(0);
            nudAddTime.Value = 0;
            _OK = false;
            this.ShowDialog();
            if (_OK)
                return Convert.ToInt32(nudDelay.Value);
            return -1;
        }

        #endregion
    }
}
